<template>
  <div>
    <div class="header" v-if="!user">
      <div class="loginBox" @click="$router.push('/login')">
        <img src="../../assets/mobile.png" alt="" />
        <span>登录 / 注册</span>
      </div>
    </div>
    <div class="header login" v-else>
      <div class="top">
        <div class="left">
          <van-image class="img" fit="cover" round :src="userInfo.photo" />
          <span class="nickname">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button size="mini" round class="edit" to="/user/userinfo"
            >编辑资料</van-button
          >
        </div>
      </div>
      <div class="bottom">
        <div class="countItem">
          <span class="count">{{ userInfo.art_count }}</span>
          <span class="text">头条</span>
        </div>
        <div class="countItem">
          <span class="count">{{ userInfo.follow_count }}</span>
          <span class="text">关注</span>
        </div>
        <div class="countItem">
          <span class="count">{{ userInfo.fans_count }}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="countItem">
          <span class="count">{{ userInfo.like_count }}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <!-- 宫格导航 -->
    <van-grid column-num="2" clickable>
      <van-grid-item>
        <i slot="icon" class="iconfont iconshoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="iconfont iconlishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <van-cell is-link title="消息通知" />
    <van-cell is-link title="小智同学" />
    <van-cell class="exit" v-if="user" @click="exit">
      <span slot="default">退出登录</span>
    </van-cell>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  data () {
    return {
      userInfo: {}
    }
  },

  created () {
    if (this.user) {
      this.renderUserInfo()
    }
  },

  methods: {
    // 退出
    exit () {
      this.$dialog
        .confirm({
          title: '提示',
          message: '确定退出当前登录吗？'
        })
        .then(() => {
          // 确定退出逻辑
          // 清除token和本地的user
          this.$store.commit('setUser', null)
        })
    },
    // 获取用户自己信息
    async renderUserInfo () {
      try {
        const res = await getUserInfo()
        this.userInfo = res.data.data
      } catch (error) {
        this.$toast('获取用户信息失败，请稍后再试！')
      }
    }
  },

  computed: {
    ...mapState(['user'])
  }
}
</script>

<style scoped lang="less">
.header {
  width: 100%;
  height: 400px;
  background: url('../../assets/banner.png');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}
.loginBox {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 28px;
  flex-direction: column;
  color: #fff;
  img {
    width: 132px;
    height: 132px;
    margin-bottom: 15px;
  }
}

.login {
  display: flex;
  flex-direction: column;
  .top {
    display: flex;
    align-items: center;
    width: 100%;
    height: 231px;
    padding: 116px 33px 23px;
    box-sizing: border-box;
    justify-content: space-between;
    margin-bottom: 15px;
    .left {
      display: flex;
      align-items: center;
      color: #fff;
      font-weight: 700;
      .img {
        width: 132px;
        height: 132px;
        border: 3px solid #fff;
        margin-right: 22px;
      }
      .nickname {
        font-size: 30px;
      }
    }
  }
  .bottom {
    display: flex;
    width: 100%;
    height: 130px;
    color: #fff;
    text-align: center;
    align-items: center;
    .countItem {
      flex: 1;
      width: 100%;
      height: 130px;
      display: flex;
      align-content: center;
      justify-content: center;
      flex-direction: column;
      .count {
        font-size: 36px;
      }
      .text {
        font-size: 23px;
      }
    }
  }
}
.van-grid-item {
  height: 140px;
  .iconfont {
    font-size: 45px;
  }
  .iconshoucang {
    font-size: 50px;
    color: #eb5253;
  }
  .iconlishi {
    color: #ff9d1d;
  }
  span {
    font-size: 28px !important;
  }
}
.exit {
  margin-top: 10px;
}
.van-cell__value--alone {
  text-align: center;
  color: rgb(216, 120, 156);
}
</style>
